package com.ssm.web;


import com.ssm.Entity.Dept;
import com.ssm.Entity.Emp;
import com.ssm.service.EmpService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/EmpAndDept")
public class EmpController {

    @Resource
    private EmpService empService;


    @RequestMapping("/selectAllDept")
    @ResponseBody
    public ModelAndView empp(ModelAndView modelAndView){
        List<Dept> depts = empService.selectAllDept();
        System.out.println(depts);
        modelAndView.addObject("dept", depts);
        modelAndView.setViewName("bumen");
        return modelAndView;
    }

    @RequestMapping("/findEmpByDeptId")
    @ResponseBody
    public ModelAndView findEmpByDeptId(int deptId, ModelAndView modelAndView) {
        List<Emp> emps = empService.findEmpByDeptId(deptId);
        System.out.println(emps);
        modelAndView.addObject("emps", emps);
        modelAndView.setViewName("yuangong");
        return modelAndView;
    }

    @RequestMapping("/addEmp")
    @ResponseBody
    public ModelAndView addEmp(Emp emp, ModelAndView modelAndView) {
        int result = empService.addEmp(emp);
        if (result == 1) {
            modelAndView.setViewName("success");
        }
        return modelAndView;
    }

    @RequestMapping("/addDept")
    @ResponseBody
    public ModelAndView addDept(Dept dept, ModelAndView modelAndView) {
        int result = 0;
        result = empService.addDept(dept);
        if (result == 1) {
            modelAndView.setViewName("success");
        }
        return modelAndView;
    }
}
